package activities;

import bookless.pakage.R;
import bookless.pakage.UserHandler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity{

	private static Context context;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        context = LoginActivity.this;

        Button submit;
        submit = (Button)findViewById(R.id.loginSubmit);
        submit.setOnClickListener(new View.OnClickListener() {
        
        	/**
        	 * 
        	 */
        	@Override
        	public void onClick(View v) {

        		
        		EditText name = (EditText) findViewById(R.id.loginName);
        		String userName = name.getText().toString();
        		
        		EditText p = (EditText) findViewById(R.id.loginPassword);
        		String password = p.getText().toString();
        		       		
        		String response = UserHandler.login(userName, password);
        	
        		if(response.equals("ok")){
	        		startActivity(new Intent("bookless.pakage.MYPAGE"));
	        	
	        	//Print out message if and why login din't work
	        	}else if(response.equals("wrongPassword")){
	        		Dialog d = new Dialog(LoginActivity.this);
					d.setTitle("Error!");
					TextView tv = new TextView(LoginActivity.this);
					tv.setText("You wrote wrong password");
					d.setContentView(tv);
					d.show();
					
	        	}else if(response.equals("noUser")){
	        		Dialog d = new Dialog(LoginActivity.this);
					d.setTitle("Error!");
					TextView tv = new TextView(LoginActivity.this);
					tv.setText("There is no user with that user name");
					d.setContentView(tv);
					d.show();
					
	        	}else if(response.equals("wrongInData")){
	        		Dialog d = new Dialog(LoginActivity.this);
					d.setTitle("Error!");
					TextView tv = new TextView(LoginActivity.this);
					tv.setText("You must fill in both user name and password");
					d.setContentView(tv);
					d.show();
	        	}	
           	}
        });
    }
    
	public static Context getContext() {
		return context;
	}
}
